// pages/store_detail/store_detail.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id:'',
    info:'',
    worker:{},
    lists:[],
    navs:[],
    cid:'',
    curindex:0,
    host:'',
    technicians:[
      {
        image:"/images/jishi2.png",
        service:22
      },
      {
        image:"/images/jishi2.png",
        service:23
      },
      {
        image:"/images/jishi2.png",
        service:24
      },
      {
        image:"/images/jishi2.png",
        service:31
      },
      {
        image:"/images/jishi2.png",
        service:50
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     this.setData({
      shop_id:options.id
     })
     this.getShop();
     this.getServiceCategory();
     this.setData({
      host:api.host
     })
  },
  toCoupons(){
    wx.navigateTo({
      url: '/pages/coupons/coupons?shop_id='+this.data.shop_id,
    })
  },
  toBuyCard(){
    wx.navigateTo({
      url: '/pages/buy_number_card/buy_number_Card?shop_id='+this.data.shop_id,
    })
  },
  toShop(){
    wx.switchTab({
      url: '/pages/shop/shop',
    })
  },

  selectWorker(e){
    let index = e.currentTarget.dataset.index;
    let worker = this.data.info.workers[index];
    let workerLists = this.data.info.workers;
    workerLists.map(item=>{
      item.selected = false;
    })
    workerLists[index].selected = true;
    this.setData({
      'worker':worker,
      ['info.workers']:workerLists
    })
  },
  submit(){
    let worker = this.data.worker;
    if(!worker || !worker.id){
      wx.showToast({
        title: '请选择店内小哥',
        icon:'none'
      })
      return false;
    }
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 3]; // 上一个页面
    prevPage.setData({
      workerId: worker.id,
      workerTxt:worker.name+" | "+worker.phone
    });
    wx.navigateBack({
      delta: 2,
    })
  },
  getShop(){
    api.get('/common/shop/'+this.data.shop_id).then(res=>{
      this.setData({
        info:res.shop
      })
    })
  },
  openMap(){
    let that = this;
    wx.openLocation({
      latitude:parseFloat(that.data.info.latitude),
      longitude:parseFloat(that.data.info.longitude),
      name:that.data.info.name,
      address:that.data.info.address,
      scale: 18,
      fail:function(res){
        console.log(res)
      }
    })
  },
  calPhone(){
    wx.makePhoneCall({
      phoneNumber: this.data.info.phone,
    })
  },
  getServiceCategory(){
    api.get('/common/serviceCategory',{
      shop_id:this.data.shop_id
    }).then(res=>{
        this.setData({
          navs:res.items,
          cid:res.items ? res.items[0].id: ''
        })
        this.getServices();
    })
  },
  getServices(){

    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/common/services',{
      cid:this.data.cid,
      shop_id:this.data.shop_id
    }).then(res=>{
      this.setData({
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },
  click_nav(e){
    const index=e.currentTarget.dataset.index;
  
    let that = this;
    console.log(that.data.navs)
    this.setData({
      curindex:index,
      cid:that.data.navs[index].id,
      lists:[],
      page:1,
      lastPage:0
    })
    this.getServices();
  },
  click_list_item(e){
    const page_start=this.data.page_start;
    const index=e.currentTarget.dataset.index;
    let service = this.data.lists[index];
    wx.setStorageSync('selectService', service)
    if(page_start=='submit'){
        var pages = getCurrentPages(); // 获取页面栈
        var prevPage = pages[pages.length - 2]; // 上一个页面
        prevPage.setData({
          selectedService: service,
          serviceTxt:service.title+" | ￥"+service.price
        });
        wx.navigateBack({
          delta: 1,
        })
    }else{
      wx.navigateTo({
          url: '../submit_order/submit_order?price='+service.price+'&tit='+service.title+'&sid='+service.id+'&shop_id='+this.data.shop_id,
        })
    }

},
toServiceDetail(e){
  let index = e.currentTarget.dataset.index;
  let id = this.data.lists[index].id;
  wx.navigateTo({
    url: '/pages/service_details/service_details?id='+id,
    fail:function(res){
      console.log(res)
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})